package com.dongdongmovie.controller;

import com.dongdongmovie.pojo.PageResult;
import com.dongdongmovie.pojo.TabRoute;
import com.dongdongmovie.service.TabCategory_Service;
import com.dongdongmovie.service.TabRoute_Service;
import com.dongdongmovie.service.TabSeller_Service;
import com.dongdongmovie.util.RedisPermissionConstant;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("TabRoute")
public class TabRoute_Controller {

    //注入service
    @DubboReference
    private TabRoute_Service tabRoute_service;
    @DubboReference
    private TabCategory_Service tabCategory_service;
    @DubboReference
    private TabSeller_Service tabSeller_service;
    @Autowired
    private RedisTemplate redisTemplate;

    //跳转到show页面
    @RequestMapping("tzshow")
    public String tzshow(Model m) {
        m.addAttribute("TabCategory", tabCategory_service.show());
        return "TabRoute/show";
    }

    //跳转到add
    @RequestMapping("tzadd")
    public String tzadd(Model m) {
        m.addAttribute("TabCategory", tabCategory_service.show());
        m.addAttribute("TabSeller", tabSeller_service.show());
        return "TabRoute/add";
    }

    //跳转到update
    @RequestMapping("tzupdate")
    public String tzupdate(Model m, Integer rid) {
        m.addAttribute("TabCategory", tabCategory_service.show());
        m.addAttribute("TabSeller", tabSeller_service.show());
        m.addAttribute("r", tabRoute_service.showAll(rid));
        return "TabRoute/update";
    }

    //查询数据
    @ResponseBody
    @RequestMapping("show")
    public PageResult<TabRoute> show(TabRoute t, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult<TabRoute> page = tabRoute_service.show(t, pageNum, pagesize);
        return page;
    }

    //添加数据
    @ResponseBody
    @RequestMapping("add")
    public int add(TabRoute t) {
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"1",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"2",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"3",0,-1);
        return tabRoute_service.add(t);
    }

    //修改数据
    @ResponseBody
    @RequestMapping("updatebyId")
    public int updatebyId(TabRoute t) {
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"1",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"2",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"3",0,-1);
        return tabRoute_service.updatebyId(t);
    }

    //根据id删除
    @ResponseBody
    @RequestMapping("delete")
    public int delete(Integer rid) {
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"1",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"2",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"3",0,-1);
        return tabRoute_service.delete(rid);
    }

    //删除全部
    @ResponseBody
    @RequestMapping("deleteall")
    public int deleteall(@RequestParam(value = "rids[]") Integer[] rids) {
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"1",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"2",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"3",0,-1);
        return tabRoute_service.deleteall(rids);
    }

    //上架
    @ResponseBody
    @RequestMapping("putaway")
    public int putaway(Integer rid) {
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"1",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"2",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"3",0,-1);
        return tabRoute_service.updateputaway(rid);
    }

    //下架
    @ResponseBody
    @RequestMapping("soldout")
    public int soldout(Integer rid) {
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"1",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"2",0,-1);
        redisTemplate.opsForZSet().removeRange(RedisPermissionConstant.DDJX+"3",0,-1);
        return tabRoute_service.updatesoldout(rid);
    }
}
